function Device(needle) {
  var find = function(device){return navigator.userAgent.toLowerCase().indexOf(device) !== -1;};
  var device = {
    ios : function () {
      return this.iphone() || this.ipod() || this.ipad();
    },
    iphone : function () {
      return !this.windows() && find('iphone');
    },
    ipod : function () {
      return find('ipod');
    },
    ipad : function () {
      return find('ipad');
    },
    android : function () {//设备
      return !this.windows() && find('android');
    },
    androidPhone : function () {//android 手机
      return this.android() && find('mobile');
    },
    androidTablet : function () {//android 平板
      return this.android() && !find('mobile');
    },
    blackberry : function () { //黑莓
      return find('blackberry') || find('bb10') || find('rim');
    },
    blackberryPhone : function () {
      return this.blackberry() && !find('tablet');
    },
    blackberryTablet : function () {
      return this.blackberry() && find('tablet');
    },
    windows : function () {
      return find('windows');
    },
    windowsPhone : function () {
      return this.windows() && find('phone');
    },
    windowsTablet : function () {
      return this.windows() && (find('touch') && !this.windowsPhone());
    },
    fxos : function () { //Firefox
      return (find('(mobile;') || find('(tablet;')) && find('; rv:');
    },
    fxosPhone : function () {
      return this.fxos() && find('mobile');
    },
    fxosTablet : function () {
      return this.fxos() && find('tablet');
    },
    meego : function () { //诺基亚
      return find('meego');
    },
    cordova : function () {
      return window.cordova && location.protocol === 'file:';
    },
    nodeWebkit : function () {
      return typeof window.process === 'object';
    },
    mobile : function () {
      return this.androidPhone() || this.iphone() || this.ipod() || this.windowsPhone() || this.blackberryPhone() || this.fxosPhone() || this.meego();
    },
    tablet : function () {
      return this.ipad() || this.androidTablet() || this.blackberryTablet() || this.windowsTablet() || this.fxosTablet();
    },
    desktop : function () {
      return !this.tablet() && !this.mobile();
    },
    weixin : function(){
      return this.mobile() && find('micromessenger')
    },
    wxwork : function() {
      return this.mobile() && find('wxwork')
    }
  };
  return device[needle]();
}

$(document).ready(function() {
  var $responsiveBtn = $('#responsive-sidebar-trigger'),
    $sidebarMask = $('#sidebar-mask'),
    $sidebar = $('#sidebar'),
    $main = $('#main'),
    winWidth = $(window).width(),
    startX = 0,
    startY = 0,
    delta = {
      x: 0,
      y: 0,
    },
    swipeThreshold = winWidth / 3,
    toggleSideBar = function() {
      let isShow = $responsiveBtn.data('is-show'),
        mainHeight = $main.height(),
        sidebarHeight = $sidebar.outerHeight();
      $sidebar.css({ right: isShow ? -300 : 0 });
      $responsiveBtn.data('is-show', !isShow);
      if (!isShow && mainHeight < sidebarHeight) {
        $main.height(sidebarHeight);
      }
      $sidebarMask[isShow ? 'fadeOut' : 'fadeIn']().height($('body').height());
      $sidebar[isShow ? 'hide' : 'show']();
    },
    touchstart = function(e) {
      const touchs = e.targetTouches;
      startX = +touchs[0].pageX;
      startY = +touchs[0].pageY;
      delta.x = delta.y = 0;
      document.body.addEventListener('touchmove', touchmove, false);
      document.body.addEventListener('touchend', touchend, false);
    },
    touchmove = function(e) {
      const touchs = e.changedTouches;
      delta.x = +touchs[0].pageX - startX;
      delta.y = +touchs[0].pageY - startY;
      // 当水平距离大于垂直距离时，才认为是用户想滑动打开右侧栏
      if (Math.abs(delta.x) > Math.abs(delta.y)) {
        // e.preventDefault();
      }
    },
    touchend = function(e) {
      let touchs = e.changedTouches,
        isShow = $responsiveBtn.data('is-show');
      delta.x = +touchs[0].pageX - startX;
      // 右侧栏未显示&&用户touch点在屏幕右侧1/4区域内&&move距离大于阀值时，打开右侧栏
      if (!isShow && (startX > winWidth * 3 / 4) && Math.abs(delta.x) > swipeThreshold) {
        $responsiveBtn.trigger('click');
      }
      // 右侧栏显示中&&用户touch点在屏幕左侧侧1/4区域内&&move距离大于阀值时，关闭右侧栏
      if (isShow && (startX < winWidth * 1 / 4) && Math.abs(delta.x) > swipeThreshold) {
        $responsiveBtn.trigger('click');
      }
      startX = startY = 0;
      delta.x = delta.y = 0;
      document.body.removeEventListener('touchmove', touchmove, false);
      document.body.removeEventListener('touchend', touchend, false);
    };

  if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
    document.body.addEventListener('touchstart', touchstart);
  }

  $responsiveBtn.on('click', toggleSideBar);

  $sidebarMask.on('click', function() {
    $responsiveBtn.trigger('click');
  });

});
